{% extends "admin/base_site_gridpivot.html" %}
{% load i18n %}

{% block tools %}
{% if args.0 or active_tab == "inventory" %}
  {% include "common/snippet_follow.html" %}
{% endif %}
{{block.super}}
{% endblock %}

{% block tabs %}
{% if active_tab == "inventory" %}
  {% tabs "input.item" %}
{% elif args.0 %}
  {% tabs "input.buffer" %}
{% endif %}
{{block.super}}
{% endblock %}

{% block before_table %}{% if args.0 %}
<div id="graph" style="clear: both; height: 400px; padding: 10px; "></div>
{% endif %}{% endblock %}

{% block crosses %}
// dict that gives from an id the name of the cross
var field_by_id = {};
// dict that gives from the name of a cross the id
var id_by_field = {};

$(function(){
{% if args.0 %}
  // Resize top graph
  var available = $(window).height() - $("#graph").offset().top - cross_idx.length * 21 - 145;
  if (available > 350)
    available = 350;
  else if (available < 200)
    available = 200;
  $("#graph").width($(window).width()-60).height(available);
{% endif %}
  for (var i in cross) {
    field_by_id[i] = cross[i]["key"];
    id_by_field[cross[i]["key"]] = i;
  }
});

{% if args.0 or mode == "graph" %}
function drawGraphs(jsondata)
{
  $('#curerror').html("");
  {% if args.0 %}var margin = {top: 0, right: 100, bottom: 30, left: 70};
  {% else %}var margin = {top: 0, right: 0, bottom: 0, left: 70};
  {% endif %}var width = Math.max($({% if args.0 %}"#graph"{% else %}"#grid_graph"{% endif %}).width() - margin.left - margin.right, 0);
  var height = {% if args.0 %}$("#graph").height(){% else %}80{% endif %} - margin.top - margin.bottom;

  // Lookup table of displayed columns
  var fields = new Set();
  for (var i of cross_idx)
    fields.add(cross[i]["key"]);

  // Define X-axis
  var domain_x = [];
  var bucketnamelength = 0;
  for (var i in timebuckets)
  {
    domain_x.push(timebuckets[i]['name']);
    bucketnamelength = Math.max(timebuckets[i]['name'].length, bucketnamelength);
  }
  var x = d3.scale.ordinal()
    .domain(domain_x)
    .rangeRoundBands([0, width], 0);
  var x_width = x.rangeBand();
  {% if mode == "graph" and not args.0 %}graph.header(margin.left, x);{% endif %}

  // Define Y-axis
  var y = d3.scale.linear().rangeRound([height, 0]);

  // Draw all graphs
  $("#grid"){% if not args.0 %}.find(".graph"){% endif %}.each(function(index)
  {
    // Create a new SVG element
    $({% if args.0 %}$("#graph").get(0){% else %}this{% endif %}).html("");
    var svg = d3.select({% if args.0 %}$("#graph").get(0){% else %}this{% endif %})
      .append("svg")
      .attr("class","graphcell")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    // Build the data for d3
    var max_y = 0;
    var min_y = 0;
    var data = [];
    for (var bckt in timebuckets)
    {
      var tmp = jsondata['rows'][index][timebuckets[bckt]['name']];
      data.push({
	        'buffer': jsondata['rows'][index]['buffer'],
	        'bucket': bckt,
	        'startinv': tmp[id_by_field['startoh']],
	        'startohdoc': tmp[id_by_field['startohdoc']],
	        'safetystock': tmp[id_by_field['safetystock']],
	        'consumed': tmp[id_by_field['consumed']],
	        'consumed_proposed': tmp[id_by_field['consumed_proposed']],
	        'consumed_confirmed': tmp[id_by_field['consumed_confirmed']],
	        'consumedMO': tmp[id_by_field['consumedMO']],
	        'consumedDO': tmp[id_by_field['consumedDO']],
	        'consumedMO_confirmed': tmp[id_by_field['consumedMO_confirmed']],
          'consumedMO_proposed': tmp[id_by_field['consumedMO_proposed']],
	        'consumedDO_confirmed': tmp[id_by_field['consumedDO_confirmed']],
	        'consumedDO_proposed': tmp[id_by_field['consumedDO_proposed']],
	        'consumedSO': tmp[id_by_field['consumedSO']],
	        'consumedFcst': tmp[id_by_field['consumedFcst']],
	        'produced': tmp[id_by_field['produced']],
	        'produced_confirmed': tmp[id_by_field['produced_confirmed']],
	        'produced_proposed': tmp[id_by_field['produced_proposed']],
	        'producedMO': tmp[id_by_field['producedMO']],
	        'producedDO': tmp[id_by_field['producedDO']],
	        'producedPO': tmp[id_by_field['producedPO']],
	        'producedMO_proposed': tmp[id_by_field['producedMO_proposed']],
	        'producedDO_proposed': tmp[id_by_field['producedDO_proposed']],
	        'producedPO_proposed': tmp[id_by_field['producedPO_proposed']],
	        'producedMO_confirmed': tmp[id_by_field['producedMO_confirmed']],
	        'producedDO_confirmed': tmp[id_by_field['producedDO_confirmed']],
	        'producedPO_confirmed': tmp[id_by_field['producedPO_confirmed']],
	        'endinv': tmp[id_by_field['endoh']],
	        'total_in_progress': tmp[id_by_field['total_in_progress']],
	        'work_in_progress_mo': tmp[id_by_field['work_in_progress_mo']],
	        'on_order_po': tmp[id_by_field['on_order_po']],
	        'in_transit_do': tmp[id_by_field['in_transit_do']],
          'total_demand': tmp[id_by_field['total_demand']],
          'total_backlog': tmp[id_by_field['total_backlog']],
          'reasons': tmp[id_by_field['reasons']]
	        });
      for (var i in cross_idx)
      {
        var t = field_by_id[cross_idx[i]];
        if (t == 'startoh'
            || t == 'safetystock'
            || t == 'consumed'
            || t == 'produced'
            || t == 'endoh'
            || t == 'total_backlog') {
          if (tmp[cross_idx[i]] < min_y) min_y = tmp[cross_idx[i]];
          if (tmp[cross_idx[i]] > max_y) max_y = tmp[cross_idx[i]];
          }
      }
    }

    // Update the scale of the Y-axis by looking for the max value
    y.domain([min_y,max_y]);
    var y_zero = y(0);

    // Create D3 bars
    var y_top;
    var y_top_confirmed;
    svg.selectAll("g")
      .data(data)
      .enter()
      .append("g")
      .attr("transform", function(d) { return "translate(" + x(timebuckets[d['bucket']]['name']) + ",0)"; })
      .each(function(d) {
        var bucket = d3.select(this);
        if (d['produced'] > 0 && fields.has('produced'))
        {
          y_top = y(d['produced']);
          y_top_low = y(d['produced_confirmed']);
          if (d['produced_confirmed'] > 0)
            bucket.append("rect")
              .attr("width", x_width/2)
              .attr("height", y_zero - y_top_low)
              .attr("x", x_width/2)
              .attr("y", y_top_low)
              .style("fill","#113C5E");
          if (d['produced_proposed'] > 0)
            bucket.append("rect")
              .attr("width", x_width/2)
              .attr("height", y_top_low - y_top)
              .attr("x", x_width/2)
              .attr("y", y_top)
              .style("fill","#2B95EC");
        }
        if (d['consumed'] > 0 && fields.has('consumed'))
        {
          y_top = y(d['consumed']);
          y_top_low = y(d['consumed_confirmed']);
          if (d['consumed_confirmed'] > 0)
            bucket.append("rect")
              .attr("width", x_width/2)
              .attr("height", y_zero - y_top_low)
              .attr("y", y_top_low)
              .style("fill","#7B5E08");
          if (d['consumed_proposed'] > 0)
            bucket.append("rect")
              .attr("width", x_width/2)
              .attr("height", y_top_low - y_top)
              .attr("y", y_top)
              .style("fill","#F6BD0F");
        }
        // Invisible rectangle for the tooltip
      	bucket.append("rect")
	      .attr("height", height)
	      .attr("width", x_width)
        .attr("fill-opacity", function(d) {
          if (d["startinv"] >= 0 && (d["startinv"] >= d["safetystock"] || d["safetystock"] == 0))
             return 0;
          else
             return 0.2;
        })
        .attr("fill", function(d) {
          if (d["startinv"] < 0)
            var gradient_idx = 0;
          else if (d["startinv"] >= d["safetystock"] || d["safetystock"] == 0)
            return null;
          else
             var gradient_idx = Math.round(d["startinv"] / d["safetystock"] * 165);
          var grad = d3.selectAll("#gradient_" + gradient_idx);
          if (grad.size() == 0) {
            var newgrad = d3.select("#gradients")
              .append("linearGradient")
              .attr("id", "gradient_" + gradient_idx)
              .attr("x1", 0)
              .attr("x2", 0)
              .attr("y1", 0)
              .attr("y2", 1);
            newgrad.append("stop")
              .attr("offset", "0%")
              .attr("stop-color", "white")
              .attr("stop-opacity", 1);
            newgrad.append("stop")
              .attr("offset", "40%")
              .attr("stop-color", "rgb(255," + gradient_idx + ",0)")
              .attr("stop-opacity", 1);
            newgrad.append("stop")
              .attr("offset", "60%")
              .attr("stop-color", "rgb(255," + gradient_idx + ",0)")
              .attr("stop-opacity", 1);
            newgrad.append("stop")
              .attr("offset", "100%")
              .attr("stop-color", "white")
              .attr("stop-opacity", 0);
          }
          return "url(#gradient_" + gradient_idx + ")";
        })
	      .on("click", function(d) {
	          if (d3.event.defaultPrevented || (d['produced'] == 0 && d['consumed'] == 0))
	            return;
	          d3.select("#tooltip").style('display', 'none');

	          window.location = url_prefix
	            + "/data/input/operationplanmaterial/buffer/"
	            + admin_escape(d['buffer'])
	            + "/?noautofilter&flowdate__gte=" + timebuckets[d['bucket']]['startdate']
	            + "&flowdate__lt=" + timebuckets[d['bucket']]['enddate'];

	          d3.event.stopPropagation();
	        })
	      .on("mouseenter", function(d) {
          if (timebuckets[d['bucket']]['history']) {
	          var tiptext = '<div style="text-align:center; font-style:italic">{{_('Archived')}} '
	            + timebuckets[d['bucket']]['name'] + '</div>'
              + '<table><tr><td>{{_('start inventory')|capfirst}}</td><td class="text-end">'
              + grid.formatNumber(d['startinv'])
              + '</td></tr><tr><td>{{_('safety stock')|capfirst}}</td><td class="text-end">'
              + grid.formatNumber(d['safetystock']);
          }
          else {
            var tiptext = '<div style="text-align:center; font-weight:bold">'
              + timebuckets[d['bucket']]['name'] + '</div>'
	            + '<table><tr><td>{{_('start inventory days of cover')|capfirst}}</td><td class="text-end">'
	            + d['startohdoc'] + " {{_('days')}}"
	            + '</td></tr><tr><td>{{_('start inventory')|capfirst}}</td><td class="text-end">'
	            + grid.formatNumber(d['startinv']);
  	        if (d['producedPO_confirmed'])
  	          tiptext += '</td></tr><tr><td>{{_('produced by PO confirmed')|capfirst}}</td><td class="text-end">+ '
  	            + grid.formatNumber(d['producedPO_confirmed']);
  	        if (d['producedPO_proposed'])
  	          tiptext += '</td></tr><tr><td>{{_('produced by PO proposed')|capfirst}}</td><td class="text-end">+ '
  	            + grid.formatNumber(d['producedPO_proposed']);
  	        if (d['producedMO_confirmed'])
  	          tiptext += '</td></tr><tr><td>{{_('produced by MO confirmed')|capfirst}}</td><td class="text-end">+ '
  	            + grid.formatNumber(d['producedMO_confirmed']);
  	        if (d['producedMO_proposed'])
  	          tiptext += '</td></tr><tr><td>{{_('produced by MO proposed')|capfirst}}</td><td class="text-end">+ '
  	            + grid.formatNumber(d['producedMO_proposed']);
            if (d['producedDO_confirmed'])
  	          tiptext += '</td></tr><tr><td>{{_('produced by DO confirmed')|capfirst}}</td><td class="text-end">+ '
  	            + grid.formatNumber(d['producedDO_confirmed']);
            if (d['producedDO_proposed'])
  	          tiptext += '</td></tr><tr><td>{{_('produced by DO proposed')|capfirst}}</td><td class="text-end">+ '
  	            + grid.formatNumber(d['producedDO_proposed']);
  	        if (d['consumedMO_confirmed'])
  	          tiptext += '</td></tr><tr><td>{{_('consumed by MO confirmed')|capfirst}}</td><td class="text-end">- '
  	            + grid.formatNumber(d['consumedMO_confirmed']);
  	        if (d['consumedMO_proposed'])
  	          tiptext += '</td></tr><tr><td>{{_('consumed by MO proposed')|capfirst}}</td><td class="text-end">- '
  	            + grid.formatNumber(d['consumedMO_proposed']);
  	        if (d['consumedSO'])
  	          tiptext += '</td></tr><tr><td>{{_('consumed by SO')|capfirst}}</td><td class="text-end">- '
  	            + grid.formatNumber(d['consumedSO']);
  	        if (d['consumedFcst'])
  	          tiptext += '</td></tr><tr><td>{{_('consumed by Fcst')|capfirst}}</td><td class="text-end">- '
  	            + grid.formatNumber(d['consumedFcst']);
  	        if (d['consumedDO_confirmed'])
  	          tiptext += '</td></tr><tr><td>{{_('consumed by DO confirmed')|capfirst}}</td><td class="text-end">- '
  	            + grid.formatNumber(d['consumedDO_confirmed']);
  	        if (d['consumedDO_proposed'])
  	          tiptext += '</td></tr><tr><td>{{_('consumed by DO proposed')|capfirst}}</td><td class="text-end">- '
  	            + grid.formatNumber(d['consumedDO_proposed']);
  	        tiptext += '</td></tr><tr><td>{{_('end inventory')|capfirst}}</td><td class="text-end">= '
  	          + grid.formatNumber(d['startinv']+d['produced']-d['consumed']);
  	        if (d['safetystock'])
  	          tiptext += '</td></tr><tr><td>{{_('safety stock')|capfirst}}</td><td class="text-end">'
  	            + grid.formatNumber(d['safetystock']);
  	        if (d['total_in_progress'])
  	          tiptext += '</td></tr><tr><td>{{_('total in progress')|capfirst}}</td><td class="text-end">'
  	            + grid.formatNumber(d['total_in_progress']);
  	        if (d['work_in_progress_mo'])
  	          tiptext += '</td></tr><tr><td>{{_('work in progress MO')|capfirst}}</td><td class="text-end">'
  	            + grid.formatNumber(d['work_in_progress_mo']);
  	        if (d['on_order_po'])
  	          tiptext += '</td></tr><tr><td>{{_('on order PO')|capfirst}}</td><td class="text-end">'
  	            + grid.formatNumber(d['on_order_po']);
  	        if (d['in_transit_do'])
  	          tiptext += '</td></tr><tr><td>{{_('in transit DO')|capfirst}}</td><td class="text-end">'
  	            + grid.formatNumber(d['in_transit_do']);
            if (d['total_demand'])
                tiptext += '</td></tr><tr><td>{{_('total demand')|capfirst}}</td><td class="text-end">'
                  + grid.formatNumber(d['total_demand']);
            if (d['total_backlog'])
              tiptext += '</td></tr><tr><td>{{_('total backlog')|capfirst}}</td><td class="text-end">'
                + grid.formatNumber(d['total_backlog']);

            tiptext += '</td></tr></table>';

            if (d['reasons']) {
              tiptext += '<div style="text-align:center; font-weight:bold">'
                + '{{_('constraints')|capfirst}}</div><table style="white-space:nowrap">';
              for (var r of d['reasons'])
                tiptext += '<tr><td>' + r[0] + '</td><td>' + r[1] + '</td></tr>';
              tiptext += '</table>';
            }
          }

	        graph.showTooltip(tiptext);
	        })
	      .on("mouseleave", graph.hideTooltip)
	      .on("mousemove", graph.moveTooltip);
      });

    // Create D3 line
    if (fields.has('startoh'))
    {
      var line = d3.svg.line()
        .x(function(d) { return x(timebuckets[d['bucket']]['name']) + x_width / 2; })
        .y(function(d) { return y(d['startinv']); });
      svg.append("svg:path")
        .attr('class', 'graphline')
        .attr("stroke","#8BBA00")
        .attr("d", line(data));
    }
    else if (fields.has('endoh'))
    {
      var line = d3.svg.line()
        .x(function(d) { return x(timebuckets[d['bucket']]['name']) + x_width / 2; })
        .y(function(d) { return y(d['endinv']); });
      svg.append("svg:path")
        .attr('class', 'graphline')
        .attr("stroke","#8BBA00")
        .attr("d", line(data));
    }
    if (fields.has('safetystock'))
    {
      var line = d3.svg.line()
        .x(function(d) { return x(timebuckets[d['bucket']]['name']) + x_width / 2; })
        .y(function(d) { return y(d['safetystock']); });
      svg.append("svg:path")
        .attr('class', 'graphline')
        .attr("stroke","#FF0000")
        .attr("d", line(data));
    }
    if (fields.has('total_backlog'))
    {
      var line = d3.svg.line()
        .x(function(d) { return x(timebuckets[d['bucket']]['name']) + x_width / 2; })
        .y(function(d) { return y(d['total_backlog']); });
      svg.append("svg:path")
        .attr('class', 'graphline')
        .attr("stroke","#000000")
        .attr("d", line(data));
    }

    // Display Y-Axis
    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickFormat(d3.format("s"));
    {% if not args.0 %}
    svg.append("g")
      .attr("class", "miniaxis")
      .call(graph.miniAxis.bind(yAxis));
    {% else %}
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

    // Display 0 line
    if (min_y < 0 && max_y > 0)
      svg.append("line")
        .attr("x1", 0)
        .attr("x2", width)
        .attr("y1", y(0))
        .attr("y2", y(0))
        .attr("stroke-width", 1)
        .attr("stroke", "black")
        .attr("shape-rendering", "crispEdges");

    // Display X-axis for a single buffer
    var nth = Math.ceil(timebuckets.length / width * bucketnamelength * 10);
    var myticks = [];
    for (var i in timebuckets)
      if (i % nth == 0) myticks.push(timebuckets[i]['name']);
    var xAxis = d3.svg.axis()
      .scale(x)
      .tickValues(myticks)
      .orient("bottom");
    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

    // Define legend
    var code_legend = [];
    if (cross_idx.includes(parseInt(id_by_field["startoh"])))
       code_legend.push(['{{_('start inventory')|capfirst}}',"#8BBA00"]);
    else if (cross_idx.includes(parseInt(id_by_field["endoh"])))
       code_legend.push(['{{_('end inventory')|capfirst}}',"#8BBA00"]);
    if (cross_idx.includes(parseInt(id_by_field["safetystock"])))
       code_legend.push(['{{_('safety stock')|capfirst}}',"#FF0000"]);
    code_legend.push(['{{_('consumed')|capfirst}}',"#F6BD0F"]);
    code_legend.push(['{{_('consumed confirmed')|capfirst}}',"#7B5E08"]);
    code_legend.push(['{{_('produced')|capfirst}}',"#2B95EC"]);
    code_legend.push(['{{_('produced confirmed')|capfirst}}',"#113C5E"]);
    if (cross_idx.includes(parseInt(id_by_field["total_backlog"])))
      code_legend.push(['{{_('total backlog')|capfirst}}',"#000000"]);

    // Display legend
    var legend = svg.append("g");
    var cnt = 0;
    for (var i of code_legend) {
      legend.append("rect")
        .attr("x", width + 82)
        .attr("width", 18)
        .attr("height", 18)
        .style("fill", i[1])
        .attr("transform", "translate(0," + (cnt*20+10) + ")");
      legend.append("text")
        .attr("x", width + 76)
        .attr("y", 9)
        .attr("dy", ".35em")
        .style("text-anchor", "end")
        .text(i[0])
        .attr("transform", "translate(0," + (cnt*20+10) + ")");
      ++cnt;
    }{% endif %}
    });
}
{% endif %}
{% if args.0 or mode == "table" %}
var last_currentdate = new moment.utc("{{request.current_date}}");

function crosses (cellvalue, options, rowdata)
{
  // Cell background
  var oh = cellvalue[id_by_field['startoh']];
  var safety = cellvalue[id_by_field['safetystock']];
  if (oh < 0 && !options.colModel.history)
     var result = '<div style="margin: -5px; background: linear-gradient(white 0%, rgba(255,0,0,0.2) 40%, rgba(255,0,0,0.2) 60%, #f0f0f0 100%)">';
  else if (oh >= safety || safety == 0 || options.colModel.history)
     var result = '<div>';
  else
     var result = '<div style="margin: -5px; background: linear-gradient(white 0%, rgba(255,'
     + Math.round(oh / safety * 165)
     + ',0,0.2) 40%, rgba(255,'
     + Math.round(oh / safety * 165)
     + ',0,0.2) 60%, white 100%)">';

  for (var i in cross_idx)
    switch(field_by_id[cross_idx[i]])
    {
      case 'startohdoc':
        if (!options.colModel.history) result += cellvalue[cross_idx[i]];
        result += '<br>';
        break;
      case 'consumed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__lt=0&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'consumed_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumed_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumed_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__lt=0&amp;operationplan__status__in=confirmed,approved,completed&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'consumed_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumed_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumed_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__lt=0&amp;operationplan__status=proposed&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'consumedMO':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedMO']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumedMO']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
           admin_escape(rowdata['buffer']) +
           "/?flowdate__gte=" + options['colModel']['startdate'] +
           "&amp;flowdate__lt=" + options['colModel']['enddate'] +
           "&amp;quantity__lt=0&amp;operationplan__type=MO&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'consumedMO_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedMO_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumedMO_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__lt=0&amp;operationplan__status__in=confirmed,approved,completed&amp;operationplan__type=MO&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'consumedMO_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedMO_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumedMO_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__lt=0&amp;operationplan__status=proposed&amp;operationplan__type=MO&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'consumedDO':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedDO']] != 0.0)
        result += grid.formatNumber(cellvalue[id_by_field['consumedDO']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
        admin_escape(rowdata['buffer']) +
        "/?flowdate__gte=" + options['colModel']['startdate'] +
        "&amp;flowdate__lt=" + options['colModel']['enddate'] +
        "&amp;quantity__lt=0&amp;operationplan__type=DO&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'consumedDO_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedDO_confirmed']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['consumedDO_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
          admin_escape(rowdata['buffer']) +
          "/?flowdate__gte=" + options['colModel']['startdate'] +
          "&amp;flowdate__lt=" + options['colModel']['enddate'] +
          "&amp;quantity__lt=0&amp;operationplan__status__in=confirmed,approved,completed&amp;operationplan__type=DO&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'consumedDO_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedDO_proposed']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['consumedDO_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
          admin_escape(rowdata['buffer']) +
          "/?flowdate__gte=" + options['colModel']['startdate'] +
          "&amp;flowdate__lt=" + options['colModel']['enddate'] +
          "&amp;quantity__lt=0&amp;operationplan__status=proposed&amp;operationplan__type=DO&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'consumedSO':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedSO']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumedSO']]) + "<a href=\"{{request.prefix}}/data/input/deliveryorder/consumed/" +
              admin_escape(rowdata['item']) + "/" +
              admin_escape(rowdata['location']) + "/" +
              options['colModel']['startdate'] + "/" +
              options['colModel']['enddate'] + "/" +
              "?noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "&amp;orders=True\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'consumedFcst':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['consumedFcst']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['consumedFcst']]) + "<a href=\"{{request.prefix}}/data/input/deliveryorder/consumed/" +
              admin_escape(rowdata['item']) + "/" +
              admin_escape(rowdata['location']) + "/" +
              options['colModel']['startdate'] + "/" +
              options['colModel']['enddate'] + "/" +
              "?noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "&amp;orders=False\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'produced':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['produced']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['produced']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'produced_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['produced_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['produced_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;operationplan__status__in=confirmed,approved,completed&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'produced_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['produced_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['produced_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;operationplan__status=proposed&amp;noautofilter\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'producedMO':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedMO']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedMO']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;noautofilter&amp;operationplan__type=MO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'producedMO_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedMO_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedMO_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;operationplan__status__in=confirmed,approved,completed&amp;noautofilter&amp;operationplan__type=MO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'producedMO_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedMO_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedMO_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;operationplan__status=proposed&amp;noautofilter&amp;operationplan__type=MO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'producedDO':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedDO']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedDO']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;noautofilter&amp;operationplan__type=DO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'producedDO_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedDO_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedDO_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;operationplan__status__in=confirmed,approved,completed&amp;noautofilter&amp;operationplan__type=DO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'producedDO_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedDO_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedDO_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;operationplan__status=proposed&amp;noautofilter&amp;operationplan__type=DO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'producedPO':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedPO']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedPO']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;noautofilter&amp;operationplan__type=PO" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'producedPO_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedPO_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedPO_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;noautofilter&amp;operationplan__type=PO&amp;operationplan__status__in=confirmed,approved,completed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'producedPO_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['producedPO_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['producedPO_proposed']]) + "<a href=\"{{request.prefix}}/data/input/operationplanmaterial/buffer/" +
              admin_escape(rowdata['buffer']) +
              "/?flowdate__gte=" + options['colModel']['startdate'] +
              "&amp;flowdate__lt=" + options['colModel']['enddate'] +
              "&amp;quantity__gte=0&amp;noautofilter&amp;operationplan__type=PO&amp;operationplan__status=proposed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&operationplan__batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'work_in_progress_mo':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['work_in_progress_mo']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['work_in_progress_mo']]) + "<a href=\"{{request.prefix}}/data/input/manufacturingorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'work_in_progress_mo_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['work_in_progress_mo_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['work_in_progress_mo_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/manufacturingorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?operationplan__status__in=confirmed,approved,completed&amp;noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'work_in_progress_mo_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['work_in_progress_mo_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['work_in_progress_mo_proposed']]) + "<a href=\"{{request.prefix}}/data/input/manufacturingorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?operationplan__status=proposed&amp;noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'on_order_po':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['on_order_po']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['on_order_po']]) + "<a href=\"{{request.prefix}}/data/input/purchaseorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'proposed_ordering':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['proposed_ordering']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['proposed_ordering']]) + "<a href=\"{{request.prefix}}/data/input/purchaseorder/?noautofilter" +
              "&amp;item=" + admin_escape(rowdata['item']) +
              "&amp;location=" + admin_escape(rowdata['location']) +
              "&amp;startdate__lt=" + options['colModel']['enddate'] +
              "&amp;startdate__gte=" + options['colModel']['startdate'] +
              "&amp;status=proposed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'on_order_po_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['on_order_po_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['on_order_po_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/purchaseorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter&amp;operationplan__status__in=confirmed,approved,completed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'on_order_po_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['on_order_po_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['on_order_po_proposed']]) + "<a href=\"{{request.prefix}}/data/input/purchaseorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter&amp;operationplan__status=proposed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
      case 'in_transit_do':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['in_transit_do']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['in_transit_do']]) + "<a href=\"{{request.prefix}}/data/input/distributionorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'in_transit_do_confirmed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['in_transit_do_confirmed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['in_transit_do_confirmed']]) + "<a href=\"{{request.prefix}}/data/input/distributionorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter&amp;operationplan__status__in=confirmed,approved,completed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'in_transit_do_proposed':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['in_transit_do_proposed']] != 0.0)
           result += grid.formatNumber(cellvalue[id_by_field['in_transit_do_proposed']]) + "<a href=\"{{request.prefix}}/data/input/distributionorder/operationplanmaterial/" +
              admin_escape(rowdata['item']) + "/" + admin_escape(rowdata['location']) +
              "/" + options['colModel']['enddate'] +
              "/?noautofilter&amp;operationplan__status=proposed" +
              (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
              "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'open_orders':
        var startdate = new moment.utc(options['colModel']['startdate'], datetimeformat);
        var enddate = new moment.utc(options['colModel']['enddate'], datetimeformat);
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['open_orders']] != 0.0)
        result += grid.formatNumber(cellvalue[id_by_field['open_orders']]) + "<a href=\"{{request.prefix}}/data/input/demand/" +
            "?noautofilter&amp;item=" + admin_escape(rowdata['item']) +
            "&amp;location=" + admin_escape(rowdata['location']) +
            "&amp;due__gte=" +
            (last_currentdate >= startdate && last_currentdate < enddate ? moment.utc("1970-01-01").format(datetimeformat) : options['colModel']['startdate']) +
            "&amp;due__lt=" +  options['colModel']['enddate'] +
            (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
            "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'net_forecast':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['net_forecast']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['net_forecast']]) + "<a href=\"{{request.prefix}}/forecast/" +
          "?noautofilter&amp;item=" + admin_escape(rowdata['item']) +
            "&amp;location=" + admin_escape(rowdata['location']) +
            "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'total_demand':
        var startdate = new moment.utc(options['colModel']['startdate'], datetimeformat);
        var enddate = new moment.utc(options['colModel']['enddate'], datetimeformat);
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['total_demand']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['total_demand']]) + "<a href=\"{{request.prefix}}/data/input/deliveryorder/" +
          "?noautofilter&amp;item=" + admin_escape(rowdata['item']) +
            "&amp;location=" + admin_escape(rowdata['location']) +
            "&amp;demand__due__gte=" +
            (last_currentdate >= startdate && last_currentdate < enddate ? moment.utc("1970-01-01").format(datetimeformat) : options['colModel']['startdate']) +
            "&amp;demand__due__lt=" +  options['colModel']['enddate'] +
            (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
            "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'order_backlog':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['order_backlog']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['order_backlog']]) + "<a href=\"{{request.prefix}}/data/input/deliveryorder/" +
          "?noautofilter&amp;item=" + admin_escape(rowdata['item']) +
            "&amp;location=" + admin_escape(rowdata['location']) +
            "&amp;orders=True" +
            "&amp;duedate__lt=" + options['colModel']['enddate'] +
            "&amp;enddate__gte=" +  options['colModel']['enddate'] +
            (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
            "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'forecast_backlog':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['forecast_backlog']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['forecast_backlog']]) + "<a href=\"{{request.prefix}}/data/input/deliveryorder/" +
          "?noautofilter&amp;item=" + admin_escape(rowdata['item']) +
            "&amp;location=" + admin_escape(rowdata['location']) +
            "&amp;orders=False" +
            "&amp;duedate__lt=" + options['colModel']['enddate'] +
            "&amp;enddate__gte=" +  options['colModel']['enddate'] +
            (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
            "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'total_backlog':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['total_backlog']] != 0.0)
          result += grid.formatNumber(cellvalue[id_by_field['total_backlog']]) + "<a href=\"{{request.prefix}}/data/input/deliveryorder/" +
          "?noautofilter&amp;item=" + admin_escape(rowdata['item']) +
            "&amp;location=" + admin_escape(rowdata['location']) +
            "&amp;duedate__lt=" + options['colModel']['enddate'] +
            "&amp;enddate__gte=" +  options['colModel']['enddate'] +
            (rowdata['batch'] == undefined || rowdata['batch'] == ""? "":"&batch=" + admin_escape(rowdata['batch'])) +
            "\">&nbsp;<span class='context cross fa fa-caret-right'></span></a><br>";
        else
          result += '0<br>';
        break;
        case 'color':
        if (options.colModel.history)
          result += '<br>';
        else if (cellvalue[id_by_field['color']] != 0) {
          result += grid.formatNumber(rowdata['is_ip_buffer'] == "True" ? cellvalue[id_by_field['color']] : Math.abs(cellvalue[id_by_field['color']])) +
          (rowdata['is_ip_buffer'] == "True" ? " %" : (" days " + (cellvalue[id_by_field['color']] < 0 ? "early" : "late")))
          + "<br>";
        }
        else
          result += '0' + (rowdata['is_ip_buffer'] == "True" ? " %" : "") + '<br>';
        break;
      case 'startoh':
        if (cellvalue[cross_idx[i]] !== null) {
          if (oh < 0 || !(oh >= safety || safety == 0))
            result += '<span style="color: red; font-weight: bold">'+ grid.formatNumber(cellvalue[cross_idx[i]]) + '</span>';
          else
            result += grid.formatNumber(cellvalue[cross_idx[i]]);
        }
        result += '<br>';
        break;
      case 'safetystock':
        if (cellvalue[cross_idx[i]] !== null)
          result += grid.formatNumber(cellvalue[cross_idx[i]]);
        result += '<br>';
        break;
      default:
        if(!cross[cross_idx[i]].hidden) {
          if (!options.colModel.history)
            result += grid.formatNumber(cellvalue[cross_idx[i]]);
          result += '<br>';
        }
    }
  result += "</div>";
  return result;
};
{% endif %}{% endblock %}

{% block extra_grid %}{% if args.0 or mode == "graph" %}loadComplete: drawGraphs,
{% endif %}{% endblock %}

{% block after_table %}
  <svg width="0" height="0">
  <defs id="gradients">
  </defs>
</svg>
{% endblock %}